#include <errno.h>
#include <malloc.h>
#include <string.h>

#include <hardware/hardware.h>
#include <hardware/mytest.h>

static int addTest(const int a, const int b){
    printf("hal impl addTest,a = %d, b = %d, res = %d\n", a, b, a+b);
    return a + b;
}

static int mytest_close(hw_device_t *dev) {
    free(dev);
    return 0;
}

static int mytest_open(const hw_module_t* module, const char* name,hw_device_t** device) {
    printf("mytest_open name = %s\n",name);
    if (strcmp(name, MYTEST_HARDWARE_MODULE_ID) == 0) {
        mytest_device_t *dev = calloc(1, sizeof(mytest_device_t));

        dev->common.tag = HARDWARE_DEVICE_TAG;
        dev->common.version = 0;
        dev->common.module = (struct hw_module_t*) module;
        dev->common.close = mytest_close;

        /* Example settings */
        dev->addTest = addTest;

        *device = (hw_device_t*) dev;
        return 0;
    } else {
        return -EINVAL;
    }
}

static struct hw_module_methods_t mytest_module_methods = {
    .open = mytest_open,
};

struct mytest_module_t HAL_MODULE_INFO_SYM = {
    .common = {
        .tag = HARDWARE_MODULE_TAG,
        .version_major = 1,
        .version_minor = 0,
        .id = MYTEST_HARDWARE_MODULE_ID,
        .name = "Default Mytest HW HAL",
        .author = "The Android Open Source Project",
        .methods = &mytest_module_methods,
    },
};